import { RequestHandler } from 'express'
import { failure, success } from '@/utils/response'
import attributeService from '@/services/attributeService'

const getAttributes: RequestHandler = async function (req, res) {
  try {
    const data = await attributeService.getAttributes(req.query)
    success(res, '查询属性列表成功。', data)
  } catch (error) {
    failure(res, error as Error, '查询属性列表失败：')
  }
}

const getAttributeInfo: RequestHandler = async function (req, res) {
  try {
    const data = await attributeService.getAttributeInfo(+req.params.id)
    success(res, '查询属性成功。', data)
  } catch (error) {
    failure(res, error as Error, '查询属性失败：')
  }
}

const addAttributeAndValues: RequestHandler = async function (req, res) {
  try {
    const data = await attributeService.addAttributeAndValues(req.body)
    success(res, '添加属性成功。', data, 201)
  } catch (error) {
    failure(res, error as Error, '添加属性失败：')
  }
}

export default {
  getAttributes,
  getAttributeInfo,
  addAttributeAndValues,
}
